package com.happy.study.controller;

import com.alibaba.fastjson.JSONObject;
import com.happy.study.entity.*;
import com.happy.study.servier.IOptionService;
import com.happy.study.servier.IPaperService;
import com.happy.study.servier.ISubjectService;
import com.happy.study.util.ResultBody;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 21:49 2021/1/23
 * @return
 */
@Controller
@RequestMapping ( "/paper" )
public class PaperController {


    @Resource
    private IPaperService iPaperService;
    @Resource
    private ISubjectService iSubjectService;
    @Resource
    private IOptionService iOptionService;

    @RequestMapping ( "/queryList" )
    @ResponseBody
    public ResultBody<PaperEntity> paperList ( String paper ) {
        PaperEntity paperEntity = JSONObject.parseObject ( paper, PaperEntity.class );
        List<PaperEntity> paperEntities = iPaperService.queryList ( paperEntity );
        int count = iPaperService.queryCount ( paperEntity );
        return ResultBody.success ( paperEntities, count );
    }

    @GetMapping ( "/{id}" )
    public String paperOne ( @PathVariable int id, Model model ) {
        PaperEntity paperEntity = iPaperService.querySingle ( id );
        model.addAttribute ( "paperEntity", paperEntity );
        SubjectEntity subjectEntity = new SubjectEntity ();
        subjectEntity.setPaperId ( paperEntity.getPaperId () );
        List<SubjectEntity> subjectEntities = iSubjectService.queryList ( subjectEntity );
        // 获取试题和选项
        for ( SubjectEntity subject : subjectEntities ) {
            OptionEntity optionEntity = new OptionEntity ();
            optionEntity.setSubjectId ( subject.getSubjectId () );
            List<OptionEntity> optionEntities = iOptionService.queryList ( optionEntity );
            subject.setOptionEntities ( optionEntities );
        }
        model.addAttribute ( "subjectEntities", subjectEntities );
        return "paper";
    }

}
